/*
    Copyright 2006 Todi.
    Send feedback and suggestions to todi@peerfeeds.com.  
  
    This file is part of Peerfeeds.

    Peerfeeds is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    Peerfeeds is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Peerfeeds; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
  
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;

namespace Peerfeeds
{
    public partial class Manager : Form
    {
        private List<Xml.Feed> managerFeeds = new List<Xml.Feed>();

        public Manager()
        {
            InitializeComponent();

            // Load feeds initially
            Xml xml = new Xml();
            LoadFeeds(xml.ReadFeedsFromXML("Feeds.xml"));
        }

        private void closeToolStripMenuItem1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void preferencesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Preferences form = new Preferences();
            form.ShowDialog();
        }

        private void templateDesignerToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Designer form = new Designer();
            form.ShowDialog();
        }

        #region Functions for the feed importer

        private void importFeedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Import form = new Import();

            form.passControl = new Import.PassControl(PassImport);

            form.ShowDialog();
        }

        private void PassImport(object sender)
        {
            ArrayList feeds = ((ArrayList)sender);

            LoadFeeds(feeds);
        }

        #endregion

        #region Functions for the feed creator / editor

        private void addFeedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFeed();
        }

        private void editFeedToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (lsvManagerFeedList.SelectedItems.Count > 0)
            {
                OpenFeed(managerFeeds[lsvManagerFeedList.SelectedIndices[0]]);
            }
        }

        private void PassEdit(object sender)
        {
            ArrayList feeds = ((ArrayList)sender);
            // Either update or add the new feed.
        }

        private void OpenFeed()
        {
            Feed form = new Feed();

            form.passControl = new Feed.PassControl(PassEdit);

            form.ShowDialog();
        }

        private void OpenFeed(Xml.Feed feed)
        {
            Feed form = new Feed();

            form.passControl = new Feed.PassControl(PassEdit);

            form.editFeed = feed;

            form.ShowDialog();
        }

        #endregion

        private void peerfeedsWebpageToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://code.google.com/p/peerfeeds/");
        }

        private void peerfeedsWikiToolStripMenuItem_Click(object sender, EventArgs e)
        {
            System.Diagnostics.Process.Start("http://code.google.com/p/peerfeeds/w/list");
        }

        private void LoadFeeds(ArrayList feeds)
        {
            foreach (Xml.Feed feed in feeds)
            {
                ListViewItem item = new ListViewItem(feed.textCommand);
                item.Checked = feed.active;
                ListViewItem.ListViewSubItem subItem1 = new ListViewItem.ListViewSubItem(item, feed.url);
                ListViewItem.ListViewSubItem subItem2 = new ListViewItem.ListViewSubItem(item, feed.template);
                item.SubItems.Add(subItem1);
                item.SubItems.Add(subItem2);

                lsvManagerFeedList.Items.Add(item);
                managerFeeds.Add(feed);
            }

            ManagerStatusbarLabelRight.Text = lsvManagerFeedList.Items.Count.ToString() + " feeds loaded";
        }

        # region Drag and drop support for listview

        /*
          
            Borrowed from http://support.microsoft.com/kb/822483
         
         */

        private void lsvManagerFeedList_ItemDrag(object sender, ItemDragEventArgs e)
        {
            lsvManagerFeedList.DoDragDrop(lsvManagerFeedList.SelectedItems, DragDropEffects.Move);
        }

        private void lsvManagerFeedList_DragEnter(object sender, DragEventArgs e)
        {
            int len = e.Data.GetFormats().Length - 1;
            int i;
            for (i = 0; i <= len; i++)
            {
                if (e.Data.GetFormats()[i].Equals("System.Windows.Forms.ListView+SelectedListViewItemCollection"))
                {
                    //The data from the drag source is moved to the target.	
                    e.Effect = DragDropEffects.Move;
                }
            }
        }

        private void lsvManagerFeedList_DragDrop(object sender, DragEventArgs e)
        {
            //Return if the items are not selected in the ListView control.
            if (lsvManagerFeedList.SelectedItems.Count == 0)
            {
                return;
            }
            //Returns the location of the mouse pointer in the ListView control.
            Point cp = lsvManagerFeedList.PointToClient(new Point(e.X, e.Y));
            //Obtain the item that is located at the specified location of the mouse pointer.
            ListViewItem dragToItem = lsvManagerFeedList.GetItemAt(cp.X, cp.Y);
            if (dragToItem == null)
            {
                return;
            }
            //Obtain the index of the item at the mouse pointer.
            int dragIndex = dragToItem.Index;
            ListViewItem[] sel = new ListViewItem[lsvManagerFeedList.SelectedItems.Count];
            for (int i = 0; i <= lsvManagerFeedList.SelectedItems.Count - 1; i++)
            {
                sel[i] = lsvManagerFeedList.SelectedItems[i];
            }
            for (int i = 0; i < sel.GetLength(0); i++)
            {
                //Obtain the ListViewItem to be dragged to the target location.
                ListViewItem dragItem = sel[i];
                int itemIndex = dragIndex;
                if (itemIndex == dragItem.Index)
                {
                    return;
                }
                if (dragItem.Index < itemIndex)
                    itemIndex++;
                else
                    itemIndex = dragIndex + i;
                //Insert the item at the mouse pointer.
                ListViewItem insertItem = (ListViewItem)dragItem.Clone();
                lsvManagerFeedList.Items.Insert(itemIndex, insertItem);
                //Removes the item from the initial location while 
                //the item is moved to the new location.
                lsvManagerFeedList.Items.Remove(dragItem);
            }
        }

        # endregion

        private void btnManagerToolbarButtonRemove_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in lsvManagerFeedList.SelectedItems)
            {
                lsvManagerFeedList.Items.Remove(item);
            }            

            // First show the index and check state of all selected items.
            foreach (int indexChecked in lsvManagerFeedList.CheckedIndices)
            {
                lsvManagerFeedList.Items.RemoveAt(indexChecked);
                managerFeeds.RemoveAt(indexChecked);
            }
        }



    }
}